package com.wish.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wish.dto.UserDto;
import com.wish.entity.SysDept;
import com.wish.entity.SysMenu;
import com.wish.entity.SysUser;
import com.wish.entity.SysUserRole;
import com.wish.service.ISysDeptService;
import com.wish.service.ISysUserRoleService;
import com.wish.service.ISysUserService;
import com.wish.vo.ResultEntity;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 系统用户 前端控制器
 * </p>
 *
 * @author 五组
 * @since 2021-03-22
 */
@RestController
@RequestMapping("/wish/sys-user")
public class SysUserController {

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISysDeptService deptService;

    @Autowired
    private ISysUserRoleService sysUserRoleService;

    @Resource
    PasswordEncoder passwordEncoder;

    @RequestMapping("/list")
    public ResultEntity list(UserDto userDto){
        PageHelper.startPage(userDto.getPageNum(),userDto.getPageSize());
        List<SysUser> list = sysUserService.getList(userDto);
        return ResultEntity.ok(new PageInfo<>(list));
    }

    @RequestMapping("/del")
    public ResultEntity del(Integer id){
        boolean b = sysUserService.removeById(id);
        return ResultEntity.ok(b);
    }

    @RequestMapping("/delAll")
    public ResultEntity delAll(Integer[] ids){
        try{
            for (Integer id:ids) {
                sysUserService.removeById(id);
            }
            return ResultEntity.ok(true);
        }catch (Exception e){
            e.printStackTrace();
            return ResultEntity.ok(false);
        }
    }

    @RequestMapping("/add")
    public ResultEntity add(@RequestBody SysUser sysUser){
        Date date = new Date();
        sysUser.setCreateTime(date);
        String encode = passwordEncoder.encode(sysUser.getPassword());
        sysUser.setPassword(encode);
        /*//sha256加密
        String salt = RandomStringUtils.randomAlphanumeric(20);
        sysUser.setPassword(new Sha256Hash(sysUser.getPassword(), salt).toHex());
        sysUser.setSalt(salt);*/
        boolean b = sysUserService.save(sysUser);
        if(sysUser.getRids()!=null){
            for (Long rid:sysUser.getRids()) {
                SysUserRole sysUserRole = new SysUserRole();
                sysUserRole.setUserId(sysUser.getUserId());
                sysUserRole.setRoleId(rid);
                sysUserRoleService.save(sysUserRole);
            }
        }
        return ResultEntity.ok(b);
    }

    @RequestMapping("/upd")
    public ResultEntity upd(@RequestBody SysUser sysUser){
        if(sysUser.getRids()!=null){
            QueryWrapper<SysUserRole> wrapper = new QueryWrapper<>();
            wrapper.eq("user_id",sysUser.getUserId());
            sysUserRoleService.remove(wrapper);
            for (Long rid:sysUser.getRids()) {
                SysUserRole sysUserRole = new SysUserRole();
                sysUserRole.setUserId(sysUser.getUserId());
                sysUserRole.setRoleId(rid);
                sysUserRoleService.save(sysUserRole);
            }
        }
        boolean b = sysUserService.updateById(sysUser);
        return ResultEntity.ok(b);
    }

    @RequestMapping("/getOne")
    public ResultEntity getOne(Integer id){
        SysUser sysUser = sysUserService.getById(id);
        return ResultEntity.ok(sysUser);
    }




    @RequestMapping("/essave")
    public ResultEntity essave(){
        return sysUserService.essave();
    }

    /*@RequestMapping("/delAll")
    public ResultEntity delAll(Integer[] ids){
        try{
            for (Integer id:ids) {
                sysUserService.removeById(id);
            }
            return ResultEntity.ok(true);
        }catch (Exception e){
            e.printStackTrace();
            return ResultEntity.error(false);
        }

    }*/

}
